package com.example.yam.greenapp;

import android.Manifest;
import android.app.Activity;

import android.app.FragmentManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.provider.CalendarContract;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.fragment;

public class MainActivity extends AppCompatActivity {
    private TextView tv_guide_index;//首页
    private TextView tv_guide_shopcar;//购物车
    private TextView tv_guide_myself;//个人信息

    private IndexFragment indexFragment;//首页
    private ShopCarFragment shopcarFragment;//购物车
    private MyselfFragment myselfFragment;//个人信息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.tv_guide_index = (TextView) super.findViewById(R.id.tv_guid1);
        this.tv_guide_shopcar = (TextView) super.findViewById(R.id.tv_guid2);
        this.tv_guide_myself = (TextView) super.findViewById(R.id.tv_guid3);

        MainActivity.this.initFragment_index();//初始化

        this.tv_guide_index.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v){
                    MainActivity.this.changeColor(MainActivity.this.tv_guide_index);
                    MainActivity.this.initFragment_index();
                }
        });
        this.tv_guide_shopcar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                MainActivity.this.changeColor(MainActivity.this.tv_guide_shopcar);
                MainActivity.this.initFragment_shopcar();
            }
        });
        this.tv_guide_myself.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                MainActivity.this.changeColor(MainActivity.this.tv_guide_myself);
                MainActivity.this.initFragment_myself();
            }
        });
    }
    //隐藏所有的fragment
    private void hideFragment(android.app.FragmentTransaction transaction){
        if(indexFragment != null){
            transaction.hide(indexFragment);
        }
        if(shopcarFragment != null){
            transaction.hide(shopcarFragment);
        }
        if(myselfFragment != null){
            transaction.hide(myselfFragment);
        }
    }
    //显示首页
    public void initFragment_index(){
        FragmentManager fragmentManager =getFragmentManager();
        android.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
        if(MainActivity.this.indexFragment==null){
            MainActivity.this.indexFragment = new IndexFragment();
            transaction.add(R.id.fg,MainActivity.this.indexFragment);
        }
        hideFragment(transaction);
        transaction.show(indexFragment);
        transaction.commit();
    }
    //显示购物车
    public void initFragment_shopcar(){
        FragmentManager fragmentManager =getFragmentManager();
        android.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
        if(MainActivity.this.shopcarFragment==null){
            MainActivity.this.shopcarFragment = new ShopCarFragment();
            transaction.add(R.id.fg,MainActivity.this.shopcarFragment);
        }
        hideFragment(transaction);
        transaction.show(shopcarFragment);
        transaction.commit();
    }
    //显示个人信息
    public void initFragment_myself() {
        FragmentManager fragmentManager = getFragmentManager();
        android.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (MainActivity.this.myselfFragment == null) {
            MainActivity.this.myselfFragment = new MyselfFragment();
            transaction.add(R.id.fg, MainActivity.this.myselfFragment);
        }
        hideFragment(transaction);
        transaction.show(myselfFragment);
        transaction.commit();
    }
    public void changeColor(TextView tv){//修改本控件颜色为绿色
        MainActivity.this.tv_guide_index.setTextColor(MainActivity.this.getResources()
        .getColor(R.color.color_tv_guide));
        MainActivity.this.tv_guide_shopcar.setTextColor(MainActivity.this.getResources()
                .getColor(R.color.color_tv_guide));
        MainActivity.this.tv_guide_myself.setTextColor(MainActivity.this.getResources()
                .getColor(R.color.color_tv_guide));

        tv.setTextColor(MainActivity.this.getResources().getColor(R.color.color_tv_guide_HighLight));//设置高亮
    }
}
